package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.OrderFeignClient;
import com.woniu.Result;
import com.woniu.ShopFeignClient;
import com.woniu.dto.ComplainDto;
import com.woniu.dto.HandleComplainDto;
import com.woniu.pojo.Complaint;
import com.woniu.pojo.Shop;
import com.woniu.service.IComplaintService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yellow Docker
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/complain")
public class ComplaintController {

    @Autowired
    private IComplaintService complaintService;

    @Autowired
    private ShopFeignClient shopFeignClient;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @ApiOperation("修改")
    @PostMapping
    public Result update(@RequestBody Complaint complaint){
        boolean flag = complaintService.updateById(complaint);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation("提交投诉")
    @PostMapping("/other")
    public Result<Boolean> submitComplainByDto(@RequestBody ComplainDto dto){
        boolean flag= complaintService.submitComplainByDto(dto);
        return flag? Result.ok(true) : Result.fail(false);
    }

    @ApiOperation("通过orderId查找投诉")
    @GetMapping("/{orderId}")
    public Result<Complaint> getByOrderId(@PathVariable Integer orderId){
        LambdaQueryWrapper<Complaint> complaintLambdaQueryWrapper = Wrappers.lambdaQuery(Complaint.class);
        complaintLambdaQueryWrapper.eq(Complaint::getOrderId,orderId);
        Complaint complaint = complaintService.getOne(complaintLambdaQueryWrapper);
        return Result.ok(complaint);
    }

    @ApiOperation("查询门店投诉")
    @GetMapping("shop-auth/shop")
    public Result queryShopComplain(HttpServletRequest request){
        String userIdStr = request.getHeader("shopId");
        int userId = Integer.parseInt(userIdStr);
        Integer shopId = shopFeignClient.getShopId(userId).getData();
        List<Complaint> complaintList = complaintService.queryShopComplain(shopId);
        return Result.ok(complaintList);
    }

    @ApiOperation("查询后台投诉")
    @GetMapping("/back")
    public Result queryBackComplain(){
        LambdaUpdateWrapper<Complaint> complaintLambdaUpdateWrapper
                = Wrappers.lambdaUpdate(Complaint.class).eq(Complaint::getStatus, 1);
        List<Complaint> complaintList = complaintService.list(complaintLambdaUpdateWrapper);
        return Result.ok(complaintList);
    }

    @ApiOperation("查询用户投诉")
    @GetMapping("user-auth/user")
    public Result queryUserComplain(HttpServletRequest request){
        List<Complaint> complaintList = complaintService.queryUserComplain(Integer.parseInt(request.getHeader("userId")));
        return Result.ok(complaintList);

    }

    @ApiOperation("门店处理投诉")
    @PutMapping("/handle")
    public Result handleShopComplain(@RequestBody HandleComplainDto dto){
        return complaintService.handleShopComplain(dto);
    }

    @ApiOperation("用户端拒绝接受投诉处理")
    @PutMapping("user-auth/reject/{id}")
    public Result rejectComplain(@PathVariable Integer id){

        boolean result = complaintService.rejectComplain(id);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation("用户端接受投诉处理")
    @PutMapping("user-auth/accept/{id}")
    public Result acceptComplain(@PathVariable Integer id){
        boolean result = complaintService.acceptComplain(id);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation("后台处理投诉违规")
    @PutMapping("admin-auth/against/{id}")
    public Result backComplainAgainst(@PathVariable Integer id){
        Complaint complaint = complaintService.getById(id);
        if(complaint.getStatus()==2){
            return Result.fail("已处理");
        }
        complaint.setStatus(2);
        complaint.setHandleContent("已严重惩罚商家");
        complaintService.updateById(complaint);
        Integer shopId = orderFeignClient.getShopId(complaint.getOrderId()).getData();
        System.out.println(shopId);
        Shop shop = shopFeignClient.getById(shopId).getData();
        shop.setScore(shop.getScore()-1);
        String flag = shopFeignClient.updateShop(shop).getData();
        return Result.ok(flag);
    }

    @ApiOperation("后台处理投诉未违规")
    @PutMapping("admin-auth/normal/{id}")
    public Result backComplainNormal(@PathVariable Integer id){
        Complaint complaint = complaintService.getById(id);
        if(complaint.getStatus()==2){
            return Result.fail("已处理");
        }
        complaint.setHandleContent("投诉不生效");
        complaint.setStatus(2);
        complaintService.updateById(complaint);
        return Result.ok("成功处理");
    }
}

